<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>自定义多选插件</title>
  <link rel="stylesheet" href="multiselect.css">
</head>

<body>
  <h2>多选组件示例</h2>
  <div style="margin-bottom: 20px;">
    <div id="myMultiselect"></div>
  </div>
  
  <div style="margin-bottom: 20px;">
    <h3>禁用状态示例</h3>
    <div id="disabledMultiselect"></div>
  </div>
  
  <div>
    <button id="toggleDisable">切换禁用状态</button>
  </div>

  <script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
  <script src="multiselect.js"></script>
  <script>

    // // 使用示例
    // $(document).ready(function () {
    //   $('#myMultiselect').customMultiselect({
    //     placeholder: '请选择',
    //     options: [
    //       { value: '1', text: '可选项1222' },
    //       { value: '2', text: '禁用项', disabled: true },
    //       { value: '3', text: '可选项2' }
    //     ],
    //     selected: ['1']
    //   }).on('multiselect.hide', function () {
    //     console.log('多选框隐藏');
    //   })
    //   $('#myMultiselect').on('change', function() {
    //     const selectedValues = $(this).data('getCurrentValue')();
    //     console.log('选中值变化:', selectedValues);
    //     // 这里可以添加您的自定义逻辑
    // });
    // });
    // 普通多选组件初始化
    $('#myMultiselect').customMultiselect({
      placeholder: '请选择',
      options: [
        {
          value: '1',
          text: '可选项1',
          data: {
            id: '123',
            type: 'special'
          }
        },
        { value: '2', text: '禁用项', disabled: true},
        { value: '3', text: '可选项2'}
      ],
      selected: ['1']
    });

    // 禁用状态的多选组件初始化
    $('#disabledMultiselect').customMultiselect({
      placeholder: '禁用状态',
      options: [
        { value: '1', text: '选项1' },
        { value: '2', text: '选项2' ,disabled: true},
        { value: '3', text: '选项3' }
      ],
      selected: ['1'],
      disabled: true // 初始化时设置为禁用状态
    });

    // 监听选项点击事件
    $('#myMultiselect').on('multiselect.optionClick', function (e, data) {
      console.log('点击的选项:', data.value, data.element);
    });

    // 切换禁用状态按钮
    $('#toggleDisable').on('click', function() {
      const $multiselect = $('#disabledMultiselect');
      if ($multiselect.data('isDisabled')()) {
        $multiselect.data('enable')();
        $(this).text('禁用组件');
      } else {
        $multiselect.data('disable')();
        $(this).text('启用组件');
      }
    });
  </script>
</body>

</html>